﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp6
{

    public partial class Form1 : Form
    {
        private List<Word> words = new List<Word>();
        private int currentIndex = 0;
        private string connectionString = "Data Source=" +
    System.IO.Path.Combine(Application.StartupPath, "words.db3") + ";Version=3;";

        private void LoadWords()
        {
            try
            {
                using (var conn = new SQLiteConnection(connectionString))
                {
                    conn.Open();
                    string sql = "SELECT English, Chinese FROM Words";
                    using (var cmd = new SQLiteCommand(sql, conn))
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                words.Add(new Word(
                                    reader["English"].ToString(),
                                    reader["Chinese"].ToString()
                                ));
                            }
                        }
                    }
                }
                ShowNextWord();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库错误：{ex.Message}");
            }
        }
        private void ShowNextWord()
        {
            if (words.Count == 0)
            {
                lblChinese.Text = "没有可用单词";
                return;
            }

            lblChinese.Text = words[currentIndex].Chinese;
            txtInput.Clear();
            lblResult.Text = "";
            txtInput.Focus(); // 自动聚焦输入框
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadWords();
        }

        
        public class Word
        {
            public string English { get; set; }
            public string Chinese { get; set; }

            public Word(string english, string chinese)
            {
                English = english;
                Chinese = chinese;
            }
        }
        private void ShowCurrentWord()
        {
            if (words.Count == 0)
            {
                lblChinese.Text = "没有可用单词";
                txtInput.Enabled = false;
                return;
            }

            // 只更新中文提示，不清除结果
            lblChinese.Text = words[currentIndex].Chinese;
            txtInput.Clear();
            txtInput.Focus();
        }
        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && words.Count > 0)
            {
                string userInput = txtInput.Text.Trim();
                string correctAnswer = words[currentIndex].English;

                // 答案正确处理
                if (string.Equals(userInput, correctAnswer, StringComparison.OrdinalIgnoreCase))
                {
                    lblResult.Text = "正确！";
                    lblResult.ForeColor = Color.Green;
                    System.Media.SystemSounds.Asterisk.Play(); // 正确提示音

                    // 正确后才切换单词
                    currentIndex = (currentIndex + 1) % words.Count;
                    ShowCurrentWord();
                }
                // 答案错误处理
                else
                {
                    lblResult.Text = $"错误，正确答案：{correctAnswer}";
                    lblResult.ForeColor = Color.Red;
                    System.Media.SystemSounds.Hand.Play(); // 错误提示音

                    // 保持当前单词不变
                    txtInput.Clear();
                    txtInput.Focus();
                }

                e.SuppressKeyPress = true; // 阻止系统默认提示音
            }
        }

        private void lblResult_Click(object sender, EventArgs e)
        {

        }
    }
}
